<?php

namespace App\Controllers\Admin;

use App\Controllers\AdminBase;
use App\Models\UserModel;

class User extends AdminBase
{

    protected $userModel;

    public function __construct()
    {
        $this->userModel = new UserModel();
    }

    /**
     * 用户列表
     */
    public function index()
    {

        $groupid = $this->request->getGet('groupid');
        $groupid = intval($groupid);

        if (IS_POST) {
            $page = $this->request->getPost('page');
            $limit = $this->request->getPost('limit');
            $param = $this->request->getPost();
            ams_clean_xss($param);
            list($list, $total, $param) = $this->userModel->limit_page($page, $limit, 0, $param);

            $return = [
                'total' => $total,
                'data' => $list,
            ];
            exit(ams_json(0, 'ok', $return));
        }
        $this->renderer->setData([
            'groupid' => $groupid,
        ]);
        return view('user/index.html');
    }

    /**
     * 添加
     */
    public function add()
    {
        if (IS_POST) {
            $data = $this->request->getPost('data');
            $rt = $this->userModel->add($data);
            if ($rt['code'] == 0) {
                $this->addSystemLog('新增用户: [' . $rt['data']['uid'] . '-' . $rt['data']['username'] . ']');
                $this->_json(0, '操作成功', $rt['data']);
            }
            $this->_json(1, $rt['msg']);
        }

        $groups = $this->userModel->getGroups();
        $this->renderer->setData([
            'groups' => $groups,
        ]);
        return view('user/add.html');
    }

    /**
     * 修改
     */
    public function edit()
    {

        $uid = (int) $this->request->getGet('uid');
        $data = $this->userModel->getUser($uid);
        if (!$data) {
            $this->adminMsg(1, '用户不存在...');
        }

        if (IS_POST) {
            $data = $this->request->getPost('data');
            ams_clean_xss($data);
            $rt = $this->userModel->edit($uid, $data);
            if ($rt['code'] == 0) {
                $this->addSystemLog('修改用户: ' . $rt['data']['id']);
                $this->_json(0, '操作成功', $rt['data']);
            }
            $this->_json(1, $rt['msg']);
        }
        $groups = $this->userModel->getGroups();
        $this->renderer->setData([
            'groups' => $groups,
            'data' => $data,
            'id' => $uid,
        ]);
        return view('user/add.html');
    }

    //删除
    public function del()
    {
        $yes = $no = 0;
        if (IS_POST) {
            $ids = $this->request->getPost('ids');
            ams_clean_xss($ids);
            if ($ids) {
                if (is_array($ids)) {
                    foreach ($ids as $id) {
                        $rt = $this->userModel->del($id);
                        if ($rt['code'] == 0) {
                            $this->addSystemLog('删除用户:#' . $id . '#' . $rt['data']['username']);
                            $yes++;
                        } else {
                            $no++;
                        }
                    }
                } else {
                    $rt = $this->userModel->del($ids);
                    if ($rt['code'] == 0) {
                        $this->addSystemLog('删除用户:#' . $ids . '#' . $rt['data']['username']);
                        $yes++;
                    } else {
                        $no++;
                    }
                }
            } else {
                $this->_json(1, '参数错误');
            }
            $this->_json(0, '操作结果 成功：' . $yes . ',失败' . $no);
        }
    }

    //开启和关闭用户
    public function status()
    {
        !$this->is_auth('admin/goods/status') && exit(ams_json(0, '您无权限操作'));
        $goodsid = (int) $this->request->getGet('id', true);

        $data = $this->goods_model->get($goodsid);
        !$data && exit(ams_json(0, '对不起，数据被删除或者查询不存在'));

        if ($data['status'] == 1) {
            $this->goods_model->update_status($goodsid, 0);
            $this->addSystemLog('修改 用户的状态为【已启用】'); // 记录日志
            exit(ams_json(1, '操作成功，正在刷新...', $goodsid));
        } else if ($data['status'] == 0) {
            $this->goods_model->update_status($goodsid, 1);
            $this->addSystemLog('修改 用户的状态为【已关闭】'); // 记录日志
            exit(ams_json(1, '操作成功，正在刷新...', 0));
        }

    }

    //分组列表管理
    public function group()
    {
        if (IS_POST) {
            $page = $this->request->getPost('page');
            $limit = $this->request->getPost('limit');
            $param = $this->request->getPost();
            ams_clean_xss($param);
            list($list, $total, $param) = $this->userModel->limit_page_group($page, $limit, 0, $param);
            $return = [
                'total' => $total,
                'data' => $list,
            ];
            $this->_json(0, 'ok', $return);
        }
        $this->renderer->setData([]);
        return view('user/group.html');
    }

    //添加分组
    public function addGroup()
    {
        if (IS_POST) {
            $data = $this->request->getPost('data');
            $rt = $this->userModel->addGroup($data);
            if ($rt['code'] == 0) {
                $this->addSystemLog('添加用户分组:' . $rt['data']['id']);
            }
            $this->_json($rt['code'], $rt['msg'], $rt['data']);
        }
        $groups = $this->userModel->getGroupSelect();
        $this->renderer->setData([
            'groups' => $groups,
        ]);
        return view('user/group_add.html');
    }

    //修改分组
    public function editGroup()
    {
        $groupid = $this->request->getGet('id');
        if (IS_POST) {
            $data = $this->request->getPost('data');
            $rt = $this->userModel->editGroup($groupid, $data);
            if ($rt['code'] == 0) {
                $this->addSystemLog('修改用户分组:' . $groupid);
            }
            $this->_json($rt['code'], $rt['msg'], $rt['data']);
        }
        $groups = $this->userModel->getGroupSelect();
        $data = $this->userModel->getGroupById($groupid);
        $this->renderer->setData([
            'groupid' => $groupid,
            'data' => $data,
            'groups' => $groups,
        ]);
        return view('user/group_add.html');
    }

    //删除分组
    public function delGroup()
    {
        $yes = $no = 0;
        if (IS_POST) {
            $ids = $this->request->getPost('ids');
            ams_clean_xss($ids);
            if ($ids) {
                if (is_array($ids)) {
                    foreach ($ids as $id) {
                        $rt = $this->userModel->delGroup($id);
                        if ($rt['code'] == 0) {
                            $this->addSystemLog('删除用户组:#' . $id . '#' . $rt['data']['name']);
                            $yes++;
                        } else {
                            $no++;
                        }
                    }
                } else {
                    $rt = $this->userModel->delGroup($ids);
                    if ($rt['code'] == 0) {
                        $this->addSystemLog('删除用户组:#' . $ids . '#' . $rt['data']['name']);
                        $yes++;
                    } else {
                        $no++;
                    }
                }
            } else {
                $this->_json(1, '参数错误');
            }
            $this->_json(0, '操作结果 成功：' . $yes . ',失败' . $no);
        }
    }

    //设置分组
    public function setGroup()
    {
        $this->renderer->setData([]);
        return view('user/group_set.html');
    }

    /**
     * 后台授权登录
     */
    public function loginUser()
    {
        $userid = $this->request->getGet('uid');
        $code = md5($this->admin['username'] . sha1($this->admin['uid'] . $this->admin['password']));
        $this->session->setTempdata('admin_login_user', $this->admin, 300);
        $url = url('user/login/backend', ['code' => ams_authcode($userid . '-' . $code, 'ENCODE')]);
        $this->adminMsg(0, '正在登录此用户...', ['url' => $url]);exit;
    }

}
